package likou.jindian;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2021/01/10 08:14
 */
public class _08_12 {
    public static void main(String[] args) {
        solveNQueens(5);
        for (int i=0;i<res.size();i++){
            for (int j=0;j<5;j++){
                System.out.println(res.get(i).get(j));
            }
            System.out.println("--------------------------------------");
        }
    }


    static List<List<String>> res = new ArrayList<>();
    static List<String> nowList = new ArrayList<>();
    static StringBuilder now = new StringBuilder();
    public static List<List<String>> solveNQueens(int n) {
        boolean lie[] =new boolean[n];//表示某列被占用
        Arrays.fill(lie,false);
        for (int i=1;i<n;i++){
            now.append('.');
        }
        deep(n,lie,0,-2);
        return res;
    }
    /**
     * hang:到了哪一行
     */
    public static void deep(int n,boolean lie[],int hang,int frontL){
        if (nowList.size()==n){
            res.add(new ArrayList<>(nowList));
            return;
        }
        for(int i=0;i<n;i++){
            if(!lie[i] &&i!=frontL-1&&i!=frontL+1){
                now.insert(i,'Q');
                lie[i]=true;
                nowList.add(String.valueOf(now));
                now.delete(i,i+1);
                deep(n,lie,hang+1,i);
                nowList.remove(nowList.size()-1);
                lie[i]=false;
            }
        }
    }
}
